<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="工序">
          <el-select
            v-model="searchParam.processId"
            placeholder="请选择工序"
            clearable
          >
            <el-option
              v-for="item in processList"
              :key="item.id"
              :label="`${item.processCode}-${item.processName}`"
              :value="`${item.id}`"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="物料">
          <el-input
            v-model="searchParam.materialCode"
            placeholder="请选择物料"
            clearable
            readonly
            @click="showWorkOrderSelect"
          >
            <template #append>
              <el-button @click="materialReset"> 清除 </el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="批次号">
          <el-input
            v-model="searchParam.materialBatch"
            placeholder="请输入批次号"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="投料时间">
          <el-date-picker
            v-model="searchParam.finishTime"
            @change="getTime(searchParam.finishTime)"
            type="datetimerange"
            range-separator="至"
            :shortcuts="globalShortcuts"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD HH:mm:ss"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="reset"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header> </template>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="index"
          label="序号"
          width="70"
          key="id"
          align="center"
        />

        <template v-for="item in tableColumns" :key="item">
          <TableColumn v-if="item.prop && item.isShow" :column="item">
            <template v-for="slot in Object.keys($slots)" #[slot]="scope">
              <slot :name="slot" v-bind="scope"></slot>
            </template>
          </TableColumn>
        </template>
        <!-- <el-table-column
          label="物料条码"
          prop="materialBarcode"
          min-width="150"
          show-overflow-tooltip
          align="left"
        />
        <el-table-column
          label="产品名称"
          prop="materialCode"
          min-width="180"
          show-overflow-tooltip
        >
          <template #default="{ row }">
            <span v-if="row.materialCode"
              >{{ row.materialName }}
            </span>
            <span v-else></span>
          </template>
        </el-table-column>
        <el-table-column
          label="投料工序"
          prop="processesName"
          min-width="80"
          show-overflow-tooltip
          align="left"
        />
        <el-table-column
          label="物料条码"
          prop="materialBarcode"
          min-width="120"
          show-overflow-tooltip
          align="left"
        />
        <el-table-column
          label="原材料批次号"
          prop="materialBatch"
          min-width="150"
          show-overflow-tooltip
          align="left"
        />
        <el-table-column
          label="投料数量"
          prop="materialQuantity"
          min-width="70"
          show-overflow-tooltip
          align="right"
        />
        <el-table-column
          label="使用数量"
          prop="employQuantity"
          min-width="70"
          show-overflow-tooltip
          align="right"
        />
        <el-table-column
          label="数量单位"
          prop="quantityUnit"
          min-width="100"
          show-overflow-tooltip
        >
          <template #default="{ row }">
            {{ useDictionary("MeasurementUnit", row.quantityUnit) }}
          </template>
        </el-table-column>
        <el-table-column
          label="类型"
          prop="flag"
          min-width="80"
          show-overflow-tooltip
          align="center"
        >
          <template #default="{ row }">
            <span v-if="row.flag === '1'">入库</span>
            <span v-if="row.flag === '2'">出库</span>
            <span v-if="row.flag === '3'">上线</span>
            <span v-if="row.flag === '4'">统计</span>
            <span v-if="row.flag === '5'">取消下线</span>
          </template>
        </el-table-column>
        <el-table-column
          label="是否合格"
          prop="produceStorageName"
          min-width="80"
          show-overflow-tooltip
          align="left"
        >
          <template #default="{ row }">
            {{ useDictionary("YesNo", row.isValid) }}
          </template>
        </el-table-column>
        <el-table-column
          label="旧线边库"
          prop="oldProduceStorageName"
          min-width="100"
          show-overflow-tooltip
          align="left"
        />
        <el-table-column
          label="新线边库"
          prop="produceStorageName"
          min-width="100"
          show-overflow-tooltip
          align="left"
        />
        <el-table-column
          label="工作中心来源"
          prop="fromWorkCenterCode"
          min-width="100"
          show-overflow-tooltip
          align="left"
        />
        <el-table-column
          label="工作中心"
          prop="workCenterCode"
          min-width="100"
          show-overflow-tooltip
          align="left"
        />
        <el-table-column
          label="投料人"
          prop="finishUserName"
          min-width="120"
          show-overflow-tooltip
          align="left"
        />
        <el-table-column
          label="投料时间"
          prop="finishTime"
          min-width="150"
          show-overflow-tooltip
          align="center"
        /> -->
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[100, 10, 25, 50]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <!--物料选择-->
    <conten-select
      :visible="selectDialogSELECT.visible"
      @change-visible="changeVisibleSELECT"
      :plantId="searchParam.plantId"
      :brekList="brekList"
    />
    <!-- 列设置 -->
    <ColSetting
      v-model:col-setting="tableColumns"
      v-model:search-setting="searchColumns"
      @search-handle="(e:boolean) => (showSearch = e)"
    />
  </div>
</template>

<script setup lang="tsx">
import { useTable, useDownload, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";
import { useCsmSelectOptionsStore } from "@/views/eam/csm/store/csmSelectOptionsStore";
import { globalShortcuts } from "@/utils/dateUtils";

defineOptions({
  name: "EAMCSMWORKRECORD0",
  inheritAttrs: false,
});

const optionsStore = useCsmSelectOptionsStore();
const materialOptions = computed(() => optionsStore.materialOptions);
// 请求参数
const requestApi = "/mes/mesproducingmaterialrecord/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  processId: "",
  materialId: "",
  materialCode: "",
  materialBatch: "",
  finishTimeSearch: [],
  finishTimeStart: "",
  finishTimeEnd: "",
};

// 处理时间
function getTime(data: any) {
  if (data) {
    searchParam.value.finishTimeStart = data[0];
    searchParam.value.finishTimeEnd = data[1];
  } else {
    searchParam.value.finishTimeStart = "";
    searchParam.value.finishTimeEnd = "";
  }
}

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  null,
  100
);

const processList = ref([]);

//产品选择
const showWorkOrderSelect = () => {
  selectDialogSELECT.value.visible = true;
};
//选择物料弹窗
const selectDialogSELECT = ref({
  visible: false,
});
const changeVisibleSELECT = (val: boolean, data: any) => {
  selectDialogSELECT.value.visible = val;
  console.log("val", val);
  console.log("data", data);
  if (data) {
    searchParam.value.materialId = data.id;
    searchParam.value.materialCode = data.materialCode;
  }
};
// 回显的数据
const brekList = ref([]);

// 重置搜索条件
const materialReset = () => {
  searchParam.value.materialId = "";
  searchParam.value.materialCode = "";
};

const getProcessList = async () => {
  const { data } = await request({
    url: "/mes/mesprocess/getList",
    method: "post",
    data: {},
  });
  processList.value = data;
};

onMounted(() => {
  getTableList();
  // optionsStore.queryMaterialList();
  getProcessList();
});

const tableColumns = reactive([
  {
    prop: "materialBarcode",
    label: "物料条码",
    minWidth: 150,
    isShow: true,
    align: "left",
  },
  {
    prop: "materialCode",
    label: "产品名称",
    minWidth: 180,
    isShow: true,
    align: "left",
    render: (scope: any) =>
      scope.row.materialCode ? scope.row.materialName : "",
  },
  {
    prop: "processesName",
    label: "投料工序",
    minWidth: 80,
    isShow: true,
    align: "left",
  },
  {
    prop: "materialBarcode",
    label: "物料条码",
    minWidth: 120,
    isShow: true,
    align: "left",
  },
  {
    prop: "materialBatch",
    label: "原材料批次号",
    minWidth: 150,
    isShow: true,
    align: "left",
  },
  {
    prop: "materialQuantity",
    label: "投料数量",
    minWidth: 80,
    isShow: true,
    align: "right",
  },
  {
    prop: "employQuantity",
    label: "使用数量",
    minWidth: 80,
    isShow: true,
    align: "right",
  },
  {
    prop: "quantityUnit",
    label: "数量单位",
    minWidth: 100,
    isShow: true,
    align: "left",
    render: (scope: any) =>
      useDictionary("MeasurementUnit", scope.row.quantityUnit),
  },
  {
    prop: "flag",
    label: "类型",
    minWidth: 80,
    isShow: true,
    align: "center",
    render: (scope: any) => {
      switch (scope.row.flag) {
        case "1":
          return "入库";
        case "2":
          return "出库";
        case "3":
          return "上线";
        case "4":
          return "统计";
        case "5":
          return "取消下线";
        default:
          return "";
      }
    },
  },
  {
    prop: "produceStorageName",
    label: "是否合格",
    minWidth: 80,
    isShow: true,
    align: "left",
    render: (scope: any) => useDictionary("YesNo", scope.row.isValid),
  },
  {
    prop: "oldProduceStorageName",
    label: "旧线边库",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "produceStorageName",
    label: "新线边库",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "fromWorkCenterCode",
    label: "工作中心来源",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "workCenterCode",
    label: "工作中心",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "finishUserName",
    label: "投料人",
    minWidth: 120,
    isShow: true,
    align: "left",
  },
  {
    prop: "finishTime",
    label: "投料时间",
    minWidth: 150,
    isShow: true,
    align: "center",
  },
]);

const searchColumns = reactive([]);
</script>
